/**
 * 
 */
package com.gua.jbs.serviceImpl;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.gua.jbs.bean.CouponBean;
import com.gua.jbs.controller.LoginController;
import com.gua.jbs.dao.ICouponDao;
import com.gua.jbs.daoImpl.CouponDao;
import com.gua.jbs.pojo.Jbscdt;
import com.gua.jbs.service.ICouponService;
import com.gua.jbs.utility.ApplicationException;
import com.gua.jbs.utility.ExceptionMessage;

public class CouponService extends BaseService implements ICouponService {

	private static final Logger logger = Logger
			.getLogger(LoginController.class);
	private ICouponDao couponDao = null;

	public CouponService() {
		couponDao = new CouponDao();
	}

	@Override
	public boolean addCoupon(CouponBean couponBean) throws ApplicationException {
		// TODO Auto-generated method stub
		boolean isSaved = false;

		// Set the pojo to be persisted
		Jbscdt objcdt = new Jbscdt();
		objcdt.setCodeId("1");
		objcdt.setCodeName(couponBean.getName());
		objcdt.setCodeValue(null);
		objcdt.setCodeGroup(couponBean.getGroup());
		objcdt.setActive(true);
		objcdt.setCreateDate(new Date());
		objcdt.setModifyDate(new Date());
		objcdt.setExtrafield1(couponBean.getName());
		objcdt.setExtrafield2(couponBean.getFormulae());
		objcdt.setExtrafield3(null);
		objcdt.setCreatedBy("Yogesh");
		objcdt.setCreatedBy("Yogesh");
		try {
			isSaved = couponDao.saveCoupon(objcdt);

		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("CouponService : addProduct() ----> " + e.getMessage());
			throw new ApplicationException(ExceptionMessage.COUPON_SAVE_UNABLE);
		}
		return isSaved;
	}

	@Override
	public Map<Integer, Map<String, String>> getCouponType()
			throws ApplicationException {
		// TODO Auto-generated method stub
		Map<Integer, Map<String, String>> map_couponType = new LinkedHashMap<Integer, Map<String, String>>();
		try {
			map_couponType = couponDao.getCuoponTypes();
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("CouponService : getCouponType() ----> "
					+ e.getMessage());
			throw new ApplicationException(ExceptionMessage.COUPON_TYPE_UNABLE);
		}
		return map_couponType;

	}

	public CouponBean getCouponInfo(String couponCode)
			throws ApplicationException {
		Jbscdt jbscdt = null;
		CouponBean couponBean = new CouponBean();
		try {
			jbscdt = couponDao.getCouponInfoByCode(couponCode);
			couponBean.setCouponId(jbscdt.getCodeId());
			couponBean.setDiscountInPercentage(jbscdt.getExtrafield1());
			couponBean.setDiscountInValue(jbscdt.getExtrafield2());
			couponBean.setGroup(jbscdt.getCodeGroup());
			couponBean.setValue(jbscdt.getCodeValue());
			couponBean.setType(jbscdt.getExtrafield3());
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("CouponService : getCouponInfo() ----> "
					+ e.getMessage());
			throw new ApplicationException(ExceptionMessage.GET_COUPON_UNABLE);
		}
		return couponBean;
	}

}
